<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Exception\CustomException;
use App\Job\IntegralJob;
use App\Kernel\Traits\DaoTrait;
use App\Kernel\Util\UserJwtInstance;
use App\Model\Goods;
use App\Model\GoodsSku;
use App\Model\IntegralDetail;
use App\Model\OrderItem;
use App\Model\ScanOrder;

use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use function JmesPath\search;

/**
 * class
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class ScanOrderDao extends Dao
{
    /**
     * 订单状态：待支付
     *
     * @var int
     */
    const STATUS_UNPAID = 0;

    /**
     * 订单状态：已支付
     *
     * @var int
     */
    const STATUS_PAID = 1;

    use DaoTrait;

    /**
     * @inheritDoc
     */
    public function model(): string
    {
        return ScanOrder::class;
    }

    /**
     * 通过条件查询单条订单
     * @param array $map
     * @return mixed
     */
    public function first(array $map)
    {
        return ScanOrder::query()->where($map)->first();
    }

    /**
     * 通过条件查询订单
     * @param array $map
     * @return mixed
     */
    public function get(array $map)
    {
        return ScanOrder::query()
            ->with('business:id,name,phone,tel')
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->orderByDesc('created_at')
            ->select('id','created_at','amount','business_id','status','order_no','user_id')
            ->paginate(10);
    }

    /**
     * 通过条件查询商品数量
     * @param array $map
     * @return mixed
     */
    public function count(array $map)
    {
        return ScanOrder::query()->where($map)->count();
    }

    /**
     * 修改
     * @param $map
     * @param array $data
     * @return int
     */
    public function update($map, array $data)
    {
        return ScanOrder::query()->where($map)->update($data);
    }

    /**
     * 创建对象
     * @param string $order_no
     * @param array $params
     */
    public function createOrder(string $order_no, array $params)
    {
        Db::beginTransaction();
        try {

            $order = [
                'status' => self::STATUS_UNPAID,
                'user_id' => uid(),
                'order_no' => $order_no,
                'business_id' => $params['business_id'],
                'amount' => $params['amount'],
                'pay_type' => $params['channel'],
                'remark' => $params['remark'],
                'created_at' => time(),
                'updated_at' => time()
            ];

            // 创建订单
            if (!ScanOrder::query()->create($order)) {
                throw new CustomException('ORDER.CREATE_FAIL');
            }

            Db::commit();
        }
        catch (CustomException $e) {
            Db::rollBack();
            $this->error($e->getMessage());
        }
        catch (\Exception $e) {
            Db::rollBack();
            $this->error('COMMON.SYSTEM_ERROR');
        }
    }
}